# -*- coding: utf-8 -*-
"""
Created on Thu May 18 23:24:15 2017

@author: Tubin Huang
"""

STR = '<?xml version="1.0"?>\
  <resultset statement="select * from htb.test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\
  <row>\
	<field name="id">1</field>\
	<field name="name">huangtub</field>\
	<field name="age">31</field>\
  </row>\
\
  <row>\
	<field name="id">2</field>\
	<field name="name">weizj</field>\
	<field name="age">29</field>\
  </row>\
\
  <row>\
	<field name="id">1</field>\
	<field name="name">zd</field>\
	<field name="age">lf</field>\
  </row>\
\
  <row>\
	<field name="id">4</field>\
	<field name="name">ww</field>\
	<field name="age" xsi:nil="true" />\
  </row>\
</resultset>'




def interpretFromString( _str ):
    root = ET.fromstring( _str )
    result = []
    for row in root:
        rowDict = {}
        for field in row:
            rowDict[ field.attrib["name"] ] = field.text
        result.append( rowDict )
    print result

def interpretFromFile( fileName ):
    tree = ET.parse( fileName )
    root = tree.getroot()
    result = []
    for row in root:
        rowDict = {}
        for field in row:
            rowDict[ field.attrib["name"] ] = field.text
        result.append( rowDict )
    print result

if __name__ == "__main__":
    fileName = "./test.xml" 
    import xml.etree.ElementTree as ET
    f = open( "./test.xml" , "w+" )
    f.write(STR)
    f.flush()
    print interpretFromString( STR )
    print interpretFromFile( fileName )
    f.close()